package com.app.megaprosoft.modyimcarcare;

import java.util.List;

import com.itextpdf.text.pdf.PdfDiv.PositionType;
import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class CarCare_Adapter_ListPay extends ArrayAdapter<CarCare_List_Payment> {

	protected static final String LOG_TAG = CarCare_Adapter_ListPay.class.getSimpleName();
	
	private List<CarCare_List_Payment> items;
	private int layoutResourceId;
	private Context context;
	private int Position;
	
	public CarCare_Adapter_ListPay(Context context, int layoutResourceId, List<CarCare_List_Payment> items) {
		super(context, layoutResourceId, items);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		CarCare_PaymentHolder holder = null;
		 setPosition(position);
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		row = inflater.inflate(layoutResourceId, parent, false);
		if(position%2!=0){
			row.setBackgroundColor(Color.rgb(135, 206, 235));
		}else{
			row.setBackgroundColor(Color.rgb(255, 254, 238));
		}
//		row.setOnClickListener(Itemclick);
		
		holder = new CarCare_PaymentHolder();
		holder.carcarePayment = items.get(position);
		
//		holder.check.setTag(holder.carcarePayment);
		holder.check = (CheckBox)row.findViewById(R.id.pay_check);
//		setOnCheckedChangeListener(holder);
		holder.name = (TextView)row.findViewById(R.id.pay_name);
		
		holder.price = (TextView)row.findViewById(R.id.pay_price);
//		
		holder.number = (TextView)row.findViewById(R.id.pay_number);
		holder.discount = (TextView)row.findViewById(R.id.pay_discount);
		holder.total = (TextView)row.findViewById(R.id.pay_total);
		
		setOnCheckedChangeListener(holder);
		
		row.setTag(holder);

		setupItem(holder);
		return row;
	}

	private void setupItem(CarCare_PaymentHolder holder) {
		holder.check.setChecked(holder.carcarePayment.isSelected());
		holder.name.setText(holder.carcarePayment.getName());
		holder.price.setText(String.valueOf(holder.carcarePayment.getPrice()));
		holder.number.setText(String.valueOf(holder.carcarePayment.getNumber()));
		holder.discount.setText(String.valueOf(holder.carcarePayment.getDiscount()));
		holder.total.setText(String.valueOf(holder.carcarePayment.getTotal()));
	}

	public static class CarCare_PaymentHolder {
		CarCare_List_Payment carcarePayment;
		CheckBox check;
		TextView name;
		TextView number;
		TextView price;
		TextView discount;
		TextView total;
	}
	
	private void setOnCheckedChangeListener(final CarCare_PaymentHolder holder) {
		holder.check
	     .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	       @Override
	      public void onCheckedChanged(CompoundButton view,
	        boolean isChecked) {
//	       int getPosition = (Integer) view.getTag();
//	       items.get(getPosition).setSelected(view.isChecked());
	    	   if(isChecked){
	    		   
	    	   }else{
	    		   holder.carcarePayment.setSelected(true);
	    	   }
	    	   
//	    	   getPosition();
	       }
	     });
		holder.name.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(holder.carcarePayment.isSelected()){
					holder.carcarePayment.setSelected(false);
				}else{
					holder.carcarePayment.setSelected(true);
				} 
				notifyDataSetChanged();
			}
		});
		holder.discount.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				notifyDataSetChanged();
			}
		});
	}

	public int getPosition() {
		return Position;
	}

	public void setPosition(int position) {
		Position = position;
	}
	
//	private void setNumberTextListeners(final CarCare_PaymentHolder holder) {
//		holder.number.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//				try{
//					holder.carcarePayment.setNumber(Integer.valueOf(s.toString()));
//				}catch (NumberFormatException e) {
//					Log.e(LOG_TAG, "error reading double value: " + s.toString());
//				}
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
//
//			@Override
//			public void afterTextChanged(Editable s) { }
//		});
//	}
//	private OnClickListener Itemclick = new OnClickListener() {
//        public void onClick(View v) {
//        	holder = get
//        	v.check.
//        }
// }; 
}
